package com.cet.algorithm.数组;

/**
 * @program: cet-practice
 * @description: 在排序数组中寻找元素的第一个和最后一个位置
 * @author: 陈恩涛
 * @create: 2023-05-05 09:48
 **/
public class LC34 {

    public static void main(String[] args) {
        int[] a = {1, 3, 5, 5, 7, 9};
        System.out.println(getRightBorder(a, 6));
    }

    static int getRightBorder(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int rightBorder = -2; // 记录一下rightBorder没有被赋值的情况
        while (left <= right) {
            int middle = left + ((right - left) / 2);
            if (nums[middle] > target) {
                right = middle - 1;
            } else { // 寻找右边界，nums[middle] == target的时候更新left
                left = middle + 1;
                rightBorder = left;
            }
        }
        return rightBorder;
    }
}
